#ifndef GSM_H_
#define GSM_H_

#include "stm32f429xx.h"
#include "stm32f4xx_hal.h"
#include "stm32f4xx_hal_gpio.h"
#include "stm32f4xx_hal_gpio_ex.h"
#include "stm32f4xx_hal_rcc.h"
#include "stm32f4xx_hal_usart.h"
#include "main.h"
#include "stdint.h"
#include "FreeRTOS.h"
#include "task.h"
#include "stdint.h"
#include "globals.h"

#define GSM_MAX_LENG_PACK			1024
#define GSM_COMMNAD_BUF				48
#define GSM_COMMAND_CAPRION_BUF		30
#define GSM_STATE_RX_BUSY			0x1
#define GSM_STATE_TX_COMPETE		0x2
#define GSM_MAX_CONFIGURE_ATTEMPTS	10
#define GSM_CAPTION_STATE_UPDATE	0x01
#define GSM_MAX_ATTEMPT_GET_RSSI	5
#define GSM_MIN_RSSI_VALUE			25

typedef enum E_GSM
{
	E_GSM_INIT 					= 1,
	E_GSM_GET_RSSI				= 3,
	E_GSM_SET_TCP_CONNECT 		= 5,
	E_GSM_TCP_SEND 				= 6,
	E_GSM_SET_TCP_DISCONNECT	= 7,
	E_GSM_OK  					= 99,
	E_GSM_ERR 					= 100
}E_GSM;

typedef struct sGSM_caption
{
	uint8_t reply[GSM_COMMAND_CAPRION_BUF];
	uint16_t rssi;
	uint8_t state;
	uint8_t ip[4];
	uint8_t counterPack;
}sGSM_caption;

typedef struct sGSM
{
	uint8_t state;
	uint8_t stateTxRx;
	uint8_t commandTx[GSM_COMMNAD_BUF];
	uint8_t commandRx[GSM_COMMNAD_BUF];
	uint16_t commandTxLeng;
	uint8_t commandRxLeng;
	sGSM_caption caption;
}sGSM;

extern sGSM gsm;

void GSM_Task(void *pvParameters);
void GSM_periph_Init(void);
eReturn GSM_Init(void);
eReturn GSM_get_RSSI(void);
eReturn GSM_set_TCP_connect(void);
eReturn GSM_TCP_send(void);
eReturn GSM_TCP_disconnect(void);

#define GSM_USART					USART2
#define GSM_USARTx_CLK_ENABLE()     __HAL_RCC_USART2_CLK_ENABLE()
#define GSM_USART_IRQn              USART2_IRQn
#define GSM_USART_IRQHandler        USART2_IRQHandler

#define GSM_USART_BAUDRATE		115200

#define GSM_GPIO_RCC			( RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOBEN | RCC_AHB1ENR_GPIODEN )
#define GSM_GPIO_AF				GPIO_AF7_USART2

#define GSM_RESET_PORT			GPIOA
#define GSM_RESET_PIN			GPIO_PIN_3

#define GSM_PWRKEY_PORT			GPIOB
#define GSM_PWRKEY_PIN			GPIO_PIN_1

#define GSM_STATUS_PORT			GPIOB
#define GSM_STATUS_PIN			GPIO_PIN_10

#define GSM_PRESENC_PORT		GPIOB
#define GSM_PRESENC_PIN			GPIO_PIN_2

#define GSM_RXD_PORT			GPIOD
#define GSM_RXD_PIN				GPIO_PIN_5

#define GSM_TXD_PORT			GPIOD
#define GSM_TXD_PIN				GPIO_PIN_6

#define GSM_PWRKEY_PORT			GPIOB
#define GSM_PWRKEY_PIN			GPIO_PIN_1

#define GSM_PWRKEY_PORT			GPIOB
#define GSM_PWRKEY_PIN			GPIO_PIN_1

#define GSM_MIC_PORT			GPIOA
#define GSM_MIC_PIN				GPIO_PIN_0

#endif
